package gruppoSoluzione.mainService.data;

import gruppoSoluzione.mainService.ServiceDBLog;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

/**
 * Responsabilità : gestione del formato Dati 
 * 
 * @author Alessandro Martinelli
 */
public class ServiceDBData {

	/**
	 * Scrive il formato xml che descrive un db/registro in un {@link BufferedStringWriter}
	 * @param writer
	 * @param logs
	 * @throws IOException
	 */
	public void writeDB(BufferedStringWriter writer, Iterable<ServiceDBLog> logs) throws IOException{
		writer.writeln("<registro>");
			Iterator<ServiceDBLog> logsIterator=logs.iterator();
			if (logsIterator.hasNext()) {
				ServiceDBLog serviceDBLog = logsIterator.next();
				writeLogsHeader(writer, serviceDBLog);
				writeLog(writer, "\t\t\t",serviceDBLog);
				while (logsIterator.hasNext()) {
					ServiceDBLog newServiceDBLog = logsIterator.next();
					if(!serviceDBLog.isSimilar(newServiceDBLog)){
						writeLogsTrailer(writer);
						writeLogsHeader(writer, newServiceDBLog);
					}
					writeLog(writer, "\t\t\t",newServiceDBLog);
					serviceDBLog=newServiceDBLog;
				}
				writeLogsTrailer(writer);
			}
		writer.writeln("</registro>");
	}
	
	/**
	 * Scrive il formato xml che descrive un elenco di stock in un {@link BufferedStringWriter}
	 * @param writer
	 * @param logs
	 * @throws IOException
	 */
	public void writeStocks(BufferedStringWriter writer, Iterable<ServiceDBLog> logs) throws IOException{
		
		writer.writeln("<stocks>");
			Iterator<ServiceDBLog> logsIterator=logs.iterator();
			if (logsIterator.hasNext()) {
				ServiceDBLog serviceDBLog = logsIterator.next();
				writeLog(writer, "\t",serviceDBLog);
				while (logsIterator.hasNext()) {
					ServiceDBLog newServiceDBLog = logsIterator.next();
					writeLog(writer, "\t",newServiceDBLog);
					serviceDBLog=newServiceDBLog;
				}
			}
		writer.writeln("</stocks>\n");
	}
	
	/**
	 * Scrive il formato xml che descrive un elenco di operatori in un {@link BufferedStringWriter}
	 * @param writer
	 * @param logs
	 * @throws IOException
	 */
	public void writeOperatoriXML(BufferedStringWriter writer,Collection<String> operatori) throws IOException{
		writer.writeln("<operatori>");
		for (String operatore : operatori) {
			writer.writeln("\t<operatore>"+operatore+"</operatore>");
		}
		writer.writeln("</operatori>");
	}
	
	
	private void writeLogsHeader(BufferedStringWriter writer,ServiceDBLog serviceDBLog) throws IOException{
		writer.writeln("\t<log>");
		writer.writeln("\t\t<luogo>"+serviceDBLog.getLuogo()+"</luogo>");
		writer.writeln("\t\t<azione>"+serviceDBLog.getAzione()+"</azione>");
		writer.writeln("\t\t<operatore>"+serviceDBLog.getOperatore()+"</operatore>");
		writer.writeln("\t\t<stocks>");
	}
	
	private void writeLog(BufferedStringWriter writer,String tabs,ServiceDBLog serviceDBLog) throws IOException{
		writer.writeln(tabs+"<stock quantità=\""+serviceDBLog.getNumero()+"\">"+serviceDBLog.getModello()+"</stock>");
	}

	private void writeLogsTrailer(BufferedStringWriter writer) throws IOException{
		writer.writeln("\t\t</stocks>");
		writer.writeln("\t</log>");
	}
}
